package com.example.seckill.exception;

import com.example.seckill.vo.RespBean;
import com.example.seckill.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * @Author: Jason
 * @ClassName: GlobalExceptionHandler
 * @Description: 全局异常处理类
 * @Package：com.example.seckill.exception
 * @Project：seckill
 * @name：GlobalExceptionHandler
 * @Filename：GlobalExceptionHandler
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 有空继续看看，理解的不好
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception e){
        if (e instanceof GlobalException){
//            将异常转为全局异常
            GlobalException ex = (GlobalException) e;
            return RespBean.error(ex.getRespBeanEnum());
        }else if (e instanceof BindException){
            BindException ex = (BindException) e;
            RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);
            respBean.setMessage("参数校验异常:"+ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return respBean;
        }
        return RespBean.error(RespBeanEnum.LOGIN_ERROR);
    }

}
